//
//  OYZtableviewheard.m
//  开心一刻
//
//  Created by 欧阳政 on 16/4/14.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "OYZtableviewheard.h"
#import "OYZartViewModel.h"
#import "UIImageView+WebCache.h"
@interface OYZtableviewheard ()<UIScrollViewDelegate,UIGestureRecognizerDelegate>
@property(nonatomic,strong)UIPageControl *pageControl;
@property(nonatomic,strong)NSTimer *timer;
@property(nonatomic,assign)NSInteger number1;
@property(nonatomic,strong)NSArray *array1;
@end
@implementation OYZtableviewheard
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _scrollview1 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, KedWidth, KedWidth *900/1500)];
        _scrollview1.delegate = self;
        _scrollview1.contentSize = CGSizeMake(KedWidth * 5, KedWidth *900/1500);
        _scrollview1.contentOffset = CGPointMake(0, 0);
        _scrollview1.pagingEnabled = YES;
        _scrollview1.bounces = NO;
        _scrollview1.showsHorizontalScrollIndicator = NO;
        [self addSubview:_scrollview1];
        
        _label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, KedWidth *900/1500, KedWidth, 35)];
        _label1.text = @"精  选";
        _label1.textAlignment = NSTextAlignmentCenter;
        _label1.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
        [self addSubview:_label1];

    }
    return self;
}

- (void)setimage:(NSArray *)imageArray
{
    _number1 = imageArray.count;
    _array1 = imageArray;
    for (int i = 0; i < imageArray.count + 2 ; i++)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * KedWidth, 0, KedWidth, KedWidth *900/1500)];
        //第一个imageView 放的是数组里面最后一张图片
        NSInteger index = imageArray.count - 1;
        
        if (i > 0) {
            index = (i - 1) % imageArray.count;
        }
        OYZartViewModel *model = imageArray[index];
        
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.topicPic]];
        //  imageView.backgroundColor = [UIColor redColor];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappp:)];
        imageView.userInteractionEnabled = YES;
        imageView.tag = 100+i;
        tap.delegate = self;
        [imageView addGestureRecognizer:tap];
        [_scrollview1 addSubview:imageView];
    }
    //设置scrollView起始位置为第二张（数组里的第一张）
    [_scrollview1 setContentOffset:CGPointMake(KedWidth, 0) animated:NO];
    
    //添加pageControl 表示显示的是数组里第几组图片
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(KedWidth-100, KedWidth * 900 / 1500 - 50, 80, 30)];
    _pageControl.numberOfPages = imageArray.count;
    //    _pageControl.backgroundColor = [UIColor redColor];
    
    [self addSubview:_pageControl];
    //创建定时器， 自动轮播
    _timer = [NSTimer timerWithTimeInterval:4 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
        [[NSRunLoop currentRunLoop] run];
    });
    
}
- (void)tappp:(UITapGestureRecognizer *)tap
{
    UIImageView *imageview =(UIImageView *)tap.view;
    NSInteger index = _array1.count - 1;
    
    if (imageview.tag - 100 > 0) {
        index = (imageview.tag- 100 - 1) % _array1.count;
    }
    OYZartViewModel *model = _array1[index];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:model.marketingDesc]];
}
- (void)timerAction
{
    //    NSLog(@"%@", [NSThread isMainThread] ? @"YES" : @"No");
    
    //自动轮播的原理就是没过一定的时间， 自动的让scrollView往后偏移一张，所以x设置为当前的偏移量加上一张屏幕的宽度
    [_scrollview1 setContentOffset:CGPointMake(_scrollview1.contentOffset.x + KedWidth, 0) animated:YES];
    //判断，当自动轮播图轮播到最后一张（数组第一张），的时候让它跳到前面去
    if (_scrollview1.contentOffset.x == (_number1 + 1) * KedWidth)
    {
        //先调到第二张（数组第一张），因为这个时候和上一张图片相同，所以用户看不出来图片切换了，动画设置No
        [_scrollview1 setContentOffset:CGPointMake(KedWidth * 1, 0) animated:NO];
        //切换到第二张之后（数组第一张），马上偏移到第三张（数组第二张），这样就可以看到数组第一张切换到第二张的效果
        [_scrollview1 setContentOffset:CGPointMake(KedWidth * 2, 0) animated:YES];
    }
    //判断当前第几张 对pageControl赋值
    NSInteger index = _scrollview1.contentOffset.x / KedWidth;
    if (index == _number1) {
        index = 0;
    }
    _pageControl.currentPage = index;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
